const app = getApp();
Page({

    // 页面的初始数据
    data: {
        mod:1,  //1下单页，0删除页
        cartlist:[],
        default_check:1,
        check_all:1,
        total_price:0,
        total_num:0
    },

    // 页面的生命周期函数 – 监听页面加载
    onLoad(res) {
        this.getdata()
    },

    getdata(){
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'viewCart',
            data:{
                mid:app.globalData.userInfo.mid
            },
            success(e){
                var obj = []
                for(var i of e.data){
                    i.check = that.data.default_check
                    obj.push(i)
                }
                console.log(obj)
                that.sum_price(obj)
                that.setData({
                    cartlist:obj
                })
            }
        });
    },

    mod_change(){
        if(this.data.mod == 1){
            this.setData({
                mod:0
            })
        }else{
            this.setData({
                mod:1
            })
        }
    },

    checkHandle(e){
        // console.log(e.currentTarget.dataset.idx)
        var obj = this.data.cartlist;
        for(var i of obj){
            if(i.goods_id == e.currentTarget.dataset.idx){
                i.check = i.check==1?0:1
            }
        }
        // console.log(obj)
        this.sum_price(obj)
        this.setData({
            cartlist:obj
        })
        this.checkHandle_jiancha()
        // this.selectComponent('.tab').sum_counter()
    },

    checkall(e){
        var obj = this.data.cartlist;
        var check_all = this.data.check_all;
        for(var i of obj){
            i.check = check_all==1?0:1
        }
        this.sum_price(obj)
        this.setData({
            cartlist:obj,
            check_all:check_all==1?0:1
        })
        // this.selectComponent('.tab').sum_counter()
    },

    checkHandle_jiancha(){
        var obj = this.data.cartlist;
        var ifcheck0 = true;  //是否全不选
        var ifcheck1 = true;  //是否全选中
        for(var i of obj){
            if(i.check==0){
                ifcheck1 =false
            }else{
                ifcheck0 = false
            }
        }
        if(!ifcheck1){
            this.setData({
                check_all:0
            })
        }else{
            this.setData({
                check_all:1
            })
        }
    },

    less(e){
        const that = this;
        var objs = this.data.cartlist
        var obj = this.data.cartlist.find(s=>s.goods_id==e.currentTarget.dataset.idx);
        if(obj.goods_num>1){
            obj.goods_num--
            swan.request({
                url: app.globalData.urlapi + 'editCart',
                data:{
                    goods_id:obj.goods_id,
                    shop_num:obj.goods_num,
                    mid:app.globalData.userInfo.mid
                },
                success(res){
                    if(res.data.code == 1){
                        for(var i of objs){
                            if(i.goods_id == e.currentTarget.dataset.idx){
                                i=obj
                            }
                        }
                        that.sum_price(objs)
                        that.setData({
                            cartlist:objs
                        })
                        that.selectComponent('.tab').sum_counter()
                    }else{
                        swan.showToast({
                            title: res.data.msg,
                            icon:'none',
                            duration:"800"
                        });
                    }
                }
            });
        }else{
            swan.showToast({
                title:'不能再减了',
                icon:'none',
                duration:'800'
            })
        }
            
    },

    add(e){
        const that = this;
        var objs = this.data.cartlist
        var obj = this.data.cartlist.find(s=>s.goods_id==e.currentTarget.dataset.idx);
        if(obj.goods_num-obj.goods.stock<=0){
            obj.goods_num++
            swan.request({
                url: app.globalData.urlapi + 'editCart',
                data:{
                    goods_id:obj.goods_id,
                    shop_num:obj.goods_num,
                    mid:app.globalData.userInfo.mid
                },
                success(res){
                    if(res.data.code == 1){
                        for(var i of objs){
                            if(i.goods_id == e.currentTarget.dataset.idx){
                                i=obj
                            }
                        }
                        that.sum_price(objs)
                        that.setData({
                            cartlist:objs
                        })
                        that.selectComponent('.tab').sum_counter()
                    }else{
                        swan.showToast({
                            title: res.data.msg,
                            icon:'none',
                            duration:"800"
                        });
                    }
                }
            });
        }else{
            swan.showToast({
                title:'库存不足',
                icon:'none',
                duration:'800'
            })
        }
    },

    sum_price(obj){
        var total_price = 0;
        var num = 0;
        for(var i of obj){
            if(i.check == 1){
                total_price = (parseFloat(total_price) + parseFloat(i.goods.price)*i.goods_num).toFixed(2)   
                num = parseInt(num) + parseInt(i.goods_num)
            }
        }
        this.setData({
            total_price,
            total_num:num
        })
    },

    change_cart(goods_id,shop_num){
        swan.request({
            url: app.globalData.urlapi + 'editCart',
            data:{
                goods_id,
                shop_num,
                mid:app.globalData.userInfo.mid
            },
            success(e){
                console.log(e)
                if(e.data.code == 1){
                    console.log(1)
                    return 1;
                }else{
                    console.log(0)
                    return 0;
                }
            }
        });
    },

    delcart(){
        const that = this;
        var obj = this.data.cartlist;
        var arr = [];
        var now_arr = this.data.cartlist;
        var ids = '';
        var a = 0
        for(var i of obj){
            if(i.check == 1){
                arr.push(i)  
            }
        }
        for(var s of arr){
            if(a==0){
                ids = ids + s.goods_id
            }else{
                ids = ids + ',' + s.goods_id
            }
            a++
            now_arr = now_arr.filter(e=>e.goods_id!=s.goods_id)
        }
        // console.log(now_arr)
        swan.request({
            url: app.globalData.urlapi + 'deleteCart',
            data:{
                mid:app.globalData.userInfo.mid,
                ids
            },
            success(e){
                if(e.data.code == 1){
                    that.setData({
                        cartlist:now_arr
                    })
                    that.selectComponent('.tab').sum_counter()
                }
                swan.showToast({
                    title: e.data.msg
                });
            }
        });
    },

    xiadanHandle(){
        var obj = this.data.cartlist;
        var arr = [];
        var goods_arr = [];
        for(var i of obj){
            if(i.check == 1){
                arr.push(i)  
            }
        }
        for(var s of arr){
            var o = {}
            var attr = []
            // console.log(s.goods_id)
            o.id = s.goods_id
            o.price = s.goods.price
            o.num = s.goods_num
            o.name = s.goods.title
            o.pic = s.goods.pic
            for(var a in s.goods.attributes){
                var z = {}
                if(a == '尺寸'){
                    z.size = s.goods.attributes[a]
                }else{
                    z.color = s.goods.attributes[a]
                }
                attr.push(z)
            }
            o.attrs = attr
            goods_arr.push(o)
        }
        console.log(goods_arr,this.data.total_price)
        swan.navigateTo({
            url: '../npay/npay?goods='+JSON.stringify(goods_arr)+'&total='+ this.data.total_price
        });
    },

    // 页面的生命周期函数 – 监听页面初次渲染完成
    onReady(res) {

    },

    // 页面的生命周期函数 – 监听页面显示
    onShow(res) {

    },

    // 页面的生命周期函数 – 监听页面隐藏
    onHide(res) {

    },

    // 页面的生命周期函数 – 监听页面卸载
    onUnload(res) {

    },

    // 页面的生命周期函数 – 监听页面重启，单击重启按钮时触发
    onForceReLaunch(res) {

    },

    // 页面的事件处理函数 – 监听用户下拉动作
    onPullDownRefresh(res) {

    },

    // 页面的事件处理函数 – 上拉触底事件的处理函数
    onReachBottom(res) {

    },

    // 页面的事件处理函数 – 用户点击右上角转发
    onShareAppMessage(res) {

    },

    // 页面的事件处理函数 – 页面滚动触发事件的处理函数
    onPageScroll(res) {

    },

    // 页面的事件处理函数 – 当前是 tab 页时，点击 tab 时触发
    onTabItemTap(res) {

    }
});